package com.nanrui.mutualinductor.ui.page;

import android.Manifest;
import android.os.Bundle;
import android.os.Handler;

import com.batman.baselibrary.base.BaseActivity;
import com.batman.baselibrary.utils.ActivityUtils;
import com.nanrui.mutualinductor.R;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.ui.util.UIStatusBarHelper;

import timber.log.Timber;

/**
 * 启动页
 */
public class SplashActivity extends BaseActivity {

    @Override
    public int getLayoutId() {
        return R.layout.activity_splash;
    }


    @Override
    public void initViews() {
        UIStatusBarHelper.translucent(this);
    }

    @Override
    public void loadData(Bundle savedInstanceState) {
        applyPermission();

    }

    /**
     * 申请 读写/手机imie权限
     */
    private void applyPermission() {

        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions
                .requestEachCombined(Manifest.permission.READ_PHONE_STATE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION)
                .subscribe(permission -> { // will emit 1 Permission object
                    if (permission.granted) {
                        // All permissions are granted !
                        Timber.tag(TAG).d(permission.name + " permission.granted");
                        handlerPermission();
                    } else if (permission.shouldShowRequestPermissionRationale) {
                        // At least one denied permission without ask never again
                        Timber.tag(TAG).d(permission.name + " permission.shouldShowRequestPermissionRationale");
                    } else {
                        // At least one denied permission with ask never again
                        // Need to go to the settings
                        Timber.tag(TAG).d(permission.name + " permission.denied");
                    }
                });
    }

    private void handlerPermission() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                autoLogin();
            }
        }, 100);

    }


    //自动登录
    private void autoLogin() {
        toMainPage();
    }

    //主页
    private void toMainPage() {
        ActivityUtils.openActivity(this, MainActivity.class);
        finish();
    }


    @Override
    protected boolean isRegisterEventBus() {
        return false;
    }
}
